package com.springboot2.authserver.jwt.controller;

import com.springboot2.authserver.jwt.entity.User;
import com.springboot2.authserver.jwt.repository.UserRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.security.Principal;

/**
 * 用户信息控制器
 *
 * @author miaoyang
 */
@Slf4j
@RestController
public class UserController {

    @Resource
    private UserRepository userRepository;

    /**
     * 初始化用户数据
     */
//    @Autowired
    public void init() {

        PasswordEncoder encoder = PasswordEncoderFactories.createDelegatingPasswordEncoder();
        userRepository.deleteAll();

        User user = new User();
        user.setUsername("admin");
        user.setPassword(encoder.encode("admin"));
        user.setRoles(new String[]{"ROLE_ADMIN", "ROLE_USER"});
        userRepository.save(user);
    }

    @RequestMapping("/auth/user")
    public Principal getUser(Principal user) {
        log.info(user.toString());
        return user;
    }

}
